<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务名" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="发布人">
        <el-select v-model="queryParams.teacherId" value-key="id" clearable placeholder="发布人"
                   @keyup.enter.native="handleQuery">
          <el-option
            v-for="item in myteachers"
            :label="item.name"
            :value="item.id">
            <span style="float: left">{{ item.name }}</span>
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="是否完成" prop="finish">
        <el-select v-model="queryParams.finish" placeholder="请选择是否完成" clearable>
          <el-option
            v-for="dict in dict.type.sys_task_finish"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['task:mytask:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['task:mytask:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['task:mytask:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['task:mytask:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mytaskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="35" align="center"/>
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="任务名" align="center" prop="taskName"/>
      <el-table-column label="任务说明" align="center" prop="taskContent"/>

      <el-table-column label="任务人" align="center" prop="teachers.name"/>

      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="截止时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="finish" label="任务状态" width="100" sortable>
        <template scope="scope">
          <span v-if="scope.row.finish == 0" style="color: red">未完成</span>
          <span v-else-if="scope.row.finish == 1" style="color: green">已完成</span>
        </template>
      </el-table-column>

      <el-table-column label="完成说明" align="center" prop="finishContent"/>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">

        <template slot-scope="scope">
          <el-button
            v-if="scope.row.finish===0"
            v-show="display"
            size="mini"
            type="success"
            icon="el-icon-cherry"
            @click="handleFinish(scope.row)"
          >完成
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['task:mytask:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['task:mytask:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改我的任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="任务名" prop="taskName">
          <el-input v-model="form.taskName" placeholder="请输入任务名"/>
        </el-form-item>

        <el-form-item label="任务说明">
          <el-input v-model="form.taskContent" placeholder="请输入任务内容"/>
        </el-form-item>



      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 添加完成模态框 -->
    <el-dialog :title="title" :visible.sync="finishOpen" width="500px" append-to-body>
      <el-form ref="finishOpen" :model="finishForm" label-width="80px">
        <el-form-item label="任务名" prop="title">
          <el-input v-model="finishForm.taskName" placeholder="请输入任务标题"/>
        </el-form-item>

        <el-form-item label="结果描述" prop="title">
          <el-input
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
            v-model="finishForm.finishContent">

          </el-input>
        </el-form-item>

        <el-form-item label="完成时间" prop="state">
          <template>
            <div class="block">
              <!--              <span class="demonstration"></span>-->
              <el-date-picker
                v-model="finishForm.endTime"
                type="datetime"
                placeholder="选择日期时间">
              </el-date-picker>
            </div>
          </template>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFinishForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listMytask, getMytask, delMytask, addMytask, updateMytask, toFinish,listTeacher} from "@/api/teacher/mytask.js";
import {takeTask} from "@/api/teacher/task";
import data from "@/views/system/dict/data";

export default {
  name: "Mytask",
  dicts: ['sys_task_finish'],
  data() {
    return {
      display: true,
      myteachers: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 我的任务表格数据
      mytaskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      finishOpen: false,
      // 查询参数
      queryParams: {

        pageNum: 1,
        pageSize: 10,
        taskName: null,
        taskContent: null,
        teacherId: null,
        endTime: null,
        finish: null,
        finishContent: null,
        taskId: null
      },
      // 表单参数
      form: {},
      finishForm: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
    this.getTeacherList();
  },
  methods: {
    /** 查询老师列表 */
    getTeacherList() {
      this.loading = true;
      listTeacher().then(response => {
        this.myteachers = response.data;
        console.log(response + "111111111111")
      });
    },
    /** 查询我的任务列表 */
    getList() {
      this.loading = true;
      listMytask(this.queryParams).then(response => {
        this.mytaskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.finishOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        taskName: null,
        taskContent: null,
        teacherId: null,
        createTime: null,
        endTime: null,
        finish: null,
        finishContent: null,
        taskId: null
      };
      this.finishForm = {
        id: null,
        taskName: null,
        taskContent: null,
        teacherId: null,
        createTime: null,
        endTime: null,
        finish: null,
        finishContent: null,
        taskId: null
      };
      this.resetForm("form");
      this.resetForm("finishForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加我的任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMytask(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改我的任务";
      });
    },
    handleFinish(row) {
      this.reset();
      const id = row.id || this.ids
      getMytask(id).then(response => {
        this.finishForm = response.data;
        // this.finishForm.endTime = null;
        // this.finishForm.finishContent = '';
        this.finishOpen = true;
        this.title = "完成任务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMytask(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMytask(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 提交按钮 */
    submitFinishForm() {
      toFinish(this.finishForm).then(response => {
        this.$modal.msgSuccess("操作成功");
        this.finishOpen = false;
        this.getList();
      });
    },
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除我的任务编号为"' + ids + '"的数据项？')
        .then(response => {
          delMytask(row).then(res=>{
            this.$modal.msgSuccess("删除成功");
            this.getList();
          })
        })
    },

    /** 删除按钮操作 */
    // handleDelete(row) {
    //   const ids = row.id || this.ids;
    //   this.$modal.confirm('是否确认删除我的任务编号为"' + ids + '"的数据项？').then(function () {
    //     return delMytask(this.form);
    //   }).then(() => {
    //     this.getList();
    //     this.$modal.msgSuccess("删除成功");
    //   }).catch(() => {
    //   });
    // },

    /** 导出按钮操作 */
    handleExport() {
      this.download('task/mytask/export', {
        ...this.queryParams
      }, `mytask_${new Date().getTime()}.xlsx`)
    }
  }
};

</script>
